
SELECT * 
INTO TMB_TEMPORAL_DIAGNOSTICOS
FROM (
SELECT '129684' HISTORIA,1 SISTEMA,0 MEDICO1,0 MEDICO2
UNION ALL SELECT '123901',1,1,1
UNION ALL SELECT '151767',1,1,1
UNION ALL SELECT '205688',1,1,1
UNION ALL SELECT '209289',0,0,0
UNION ALL SELECT '212889',1,1,1
UNION ALL SELECT '213089',0,1,0
UNION ALL SELECT '800121',1,1,0
UNION ALL SELECT '803053',1,1,1
UNION ALL SELECT '805132',0,1,1
UNION ALL SELECT '807981',1,1,1
UNION ALL SELECT '807386',0,0,0
UNION ALL SELECT '812284',1,1,1
UNION ALL SELECT '825633',1,1,0
UNION ALL SELECT '829556',1,1,0
UNION ALL SELECT '829985',0,0,0
UNION ALL SELECT '844913',0,0,0
UNION ALL SELECT '845500',0,1,0
UNION ALL SELECT '845785',1,1,1
UNION ALL SELECT '848381',0,0,0
UNION ALL SELECT '852383',0,1,1
UNION ALL SELECT '855286',0,0,0
UNION ALL SELECT '1206508',0,0,0
UNION ALL SELECT '1206586',1,1,1
UNION ALL SELECT '1209064',1,1,1
UNION ALL SELECT '1209088',1,1,1
UNION ALL SELECT '1209233',1,1,1
UNION ALL SELECT '1210008',1,1,1
UNION ALL SELECT '1210019',1,1,1
UNION ALL SELECT '1210358',1,0,0
UNION ALL SELECT '1210994',1,1,0
UNION ALL SELECT '1211203',1,1,1
UNION ALL SELECT '1211207',0,1,0
UNION ALL SELECT '1212086',1,1,1
UNION ALL SELECT '1212364',0,1,0
UNION ALL SELECT '1212886',1,1,1
UNION ALL SELECT '1213023',1,0,1
UNION ALL SELECT '1213095',0,1,1
UNION ALL SELECT '1213553',1,1,1
UNION ALL SELECT '1213622',1,1,1
UNION ALL SELECT '1214218',0,1,0
UNION ALL SELECT '1214504',0,0,0
UNION ALL SELECT '1217413',0,0,0
UNION ALL SELECT '1215402',1,1,0
UNION ALL SELECT '1216185',1,1,1
UNION ALL SELECT '1217737',1,1,1
UNION ALL SELECT '1216188',0,0,0
UNION ALL SELECT '1216923',1,1,1
UNION ALL SELECT '1217547',0,0,0
UNION ALL SELECT '1207746',1,1,1
UNION ALL SELECT '1215188',1,1,1
UNION ALL SELECT '1215380',1,1,1
)TABLA


SELECT * FROM [dbo].[HM_HISTORIACLINICA]

UPDATE HM_HISTORIACLINICA
SET RESULTADO = SISTEMA,
	SINDROMEMETABOLICO2=MEDICO2
	--SELECT *
FROM HM_HISTORIACLINICA H INNER JOIN 
(
	SELECT HC.NUMEROHISTORIA,HC.SINDROMEMETABOLICO,TB.HISTORIA,TB.MEDICO1,TB.SISTEMA,TB.MEDICO2 
	FROM TMB_TEMPORAL_DIAGNOSTICOS TB
	INNER JOIN [dbo].[HM_HISTORIACLINICA] HC ON TB.HISTORIA = HC.NUMEROHISTORIA
	WHERE MEDICO1 = SINDROMEMETABOLICO 
	)T ON  H.NUMEROHISTORIA = T.NUMEROHISTORIA


select	ROW_NUMBER() over(order by HIS.IDHISTORIACLINICA) ID,  
			EST.DESCRIPCION, 
			HIS.NUMEROHISTORIA, 
			PAC.APELLIDOPATERNO 
			+ ', ' + PAC.NOMBRES AS NOMBRES, 
			HIS.SINDROMEMETABOLICO,
			ISNULL(HIS.RESULTADO,'') AS RESULTADO,
			ISNULL(HIS.SINDROMEMETABOLICO2,'') AS SINDROMEMETABOLICO2
	from	HM_HISTORIACLINICA HIS 
	INNER JOIN HM_PACIENTE PAC ON HIS.IDPACIENTE = PAC.IDPACIENTE
	INNER JOIN HM_ESTABLECIMIENTOSALUD EST ON HIS.IDESTABLECIMIENTOSALUD = EST.IDESTABLECIMIENTOSALUD 
	INNER JOIN (SELECT IDHISTORIACLINICA FROM HM_EVALUACIONPARADIAGNOSTICO GROUP BY IDHISTORIACLINICA ) E ON E.IDHISTORIACLINICA=HIS.IDHISTORIACLINICA
	INNER JOIN TMB_TEMPORAL_DIAGNOSTICOS TB ON HIS.NUMEROHISTORIA = TB.HISTORIA